package com.kmzx.one.project.config.listener;

import com.kmzx.one.project.entity.OrderBillMoney;
import com.kmzx.one.project.entity.UserOrder;
import com.kmzx.one.project.entity.UserPay;
import com.kmzx.one.project.entity.UserProfit;
import com.kmzx.one.project.entity.Warehouse;
import com.kmzx.one.project.entity.WarehouseDetail;
import com.kmzx.one.project.events.InvalidOrderEvent;
import com.kmzx.one.project.mapper.CustomerMapper;
import com.kmzx.one.project.mapper.OrderBillMoneyMapper;
import com.kmzx.one.project.mapper.UserOrderMapper;
import com.kmzx.one.project.mapper.UserPayMapper;
import com.kmzx.one.project.mapper.UserProfitMapper;
import com.kmzx.one.project.mapper.WarehouseMapper;
import com.kmzx.one.project.others.dto.FreightInfo;
import com.kmzx.one.project.others.dto.UserProfitInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Project selections
 * @Author: sunii
 * @Date: 2019/12/28-17:56
 */


@Component("invalidOrderEventListener")
public class InvalidOrderEventListener {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private OrderBillMoneyMapper orderBillMoneyMapper;

    @Autowired
    private UserPayMapper userPayMapper;

    @Autowired
    private UserProfitMapper userProfitMapper;

    @Autowired
    WarehouseMapper warehouseMapper;

    @Async
    @EventListener(classes = {InvalidOrderEvent.class})
    @Transactional(rollbackFor = Exception.class)
    public void defaultInvalidOrderEventListener(InvalidOrderEvent invalidOrderEvent) {
        String orderId = invalidOrderEvent.getOrderId();

        Date date = new Date();
        UserOrder userOrder = new UserOrder();
        userOrder.setId(orderId);
        userOrder.setUpdateTime(date);
        userOrder.setStates(5);

        // 更新主订单
        userOrderMapper.updateByIdSelective(userOrder);
        // 更新子订单
        userOrderMapper.batchUpdateSubOrder(orderId, 5, date);


        UserOrder order = userOrderMapper.selectById(orderId);

        BigDecimal repoMoney = order.getPayMoney();
        BigDecimal profitMoney = order.getProfitMoney();

        if(repoMoney!=null && repoMoney.compareTo(BigDecimal.ZERO)>0){
            FreightInfo freight = customerMapper.selectUserFreight(order.getUserId());
            String tempId = freight.getTempId();
            UserPay userPay = new UserPay();
            userPay.setMoney(freight.getMoney().add(repoMoney));
            userPay.setUpdateTime(date);
            userPay.setId(tempId);
            userPayMapper.updateByIdSelective(userPay);

        }

        if(profitMoney!=null && profitMoney.compareTo(BigDecimal.ZERO)>0){
            UserProfitInfo userProfitInfo = customerMapper.selectUserProfit(order.getUserId());
            UserProfit userProfit = new UserProfit();
            userProfit.setId(userProfitInfo.getTemps());
            userProfit.setUpdateTime(date);
            userProfit.setProfitMoney(userProfitInfo.getCanPay().add(profitMoney));
            userProfitMapper.updateByIdSelective(userProfit);
        }



        List<WarehouseDetail> warehouseDetails = warehouseMapper.selectWarehouseInfoByOrderId(orderId, 0);

        List<Warehouse> warehouseList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(warehouseDetails)){
            for (WarehouseDetail warehouseDetail : warehouseDetails) {
                // 取消了
                Integer order_down_number = warehouseDetail.getProductNumber();
                warehouseDetail.setStates(1);
                warehouseDetail.setUpdateTime(date);
                Warehouse warehouse = warehouseMapper.selectWarehouseByBatchSkuId(warehouseDetail.getAttributeWithCategoryId(), warehouseDetail.getWarehouseBatch());
                if(warehouse!=null){
                    Integer old_number = warehouse.getNowNumber();
                    warehouse.setNowNumber(old_number+order_down_number);
                    warehouse.setUpdateTime(date);
                    warehouseList.add(warehouse);
                }
            }
            // 将库存明细表回撤状态
            warehouseMapper.batchUpdateWarehouseDetail(warehouseDetails);
            // 将库存表回填
            warehouseMapper.batchUpdateWarehouse(warehouseList);
        }

        logger.info("超时订单,失效,订单为:{}", userOrder);
    }


}
